package com.ehsunbehravesh.mypasswords;

import com.ehsunbehravesh.mypasswords.resource.ResourceLoader;
import javax.swing.ImageIcon;

/*
 * this class implements a real time password validator
 * every time the user enters a new key in the password field
 * a method from this class will be called to check and see if the passwords 
 * entered until now match each other. For each case(match or not match)
 * a corresponding image will be displayed near the 2 password fields.
 * @author km001
 */
public class RealtimePasswordValidator {

    private String password;//first password field's data
    private String confirmationPassword; //confirmation password field's data
    private ImageIcon correctPasswords; //tick image
    private ImageIcon wrongPasswords; //x image

    /*
     * constructor, initializes the private variables
     */
    public RealtimePasswordValidator(String password, String confirmationPassword) {
        this.password = password;
        this.confirmationPassword = confirmationPassword;
        correctPasswords = new ImageIcon(ResourceLoader.class.getResource("tickIcon.png"));
        wrongPasswords = new ImageIcon(ResourceLoader.class.getResource("xIcon.png"));
    }

    /*
     * alternative constructor, initializes the private variables
     */
    public RealtimePasswordValidator() {
        this.password = null;
        this.confirmationPassword = null;
        correctPasswords = new ImageIcon(ResourceLoader.class.getResource("tickIcon.png"));
        wrongPasswords = new ImageIcon(ResourceLoader.class.getResource("xIcon.png"));
    }

    /*
     * validates the two passwords, returns the correct ImageIcon
     * will be used in the MainFrame to update the images of the password/confpassword labels
     */
    public ImageIcon validatePasswords(String password, String confirmationPassword) {

        this.password = password;
        this.confirmationPassword = confirmationPassword;

        boolean isEquals = validate();

        if (isEquals) {
            return correctPasswords;
        }
        return wrongPasswords;
    }

    /*
     * validator, just checks to see whether the two passwords that are currently entered
     * by the user are the same or not
     */
    private boolean validate() {

        if (password.equals(confirmationPassword)) {
            return true;
        }
        return false;
    }

    /*
     * Getters and setters for the private variables, could be used in future updates.
     */
    public String getConfirmationPassword() {
        return confirmationPassword;
    }

    public void setConfirmationPassword(String confirmationPassword) {
        this.confirmationPassword = confirmationPassword;
    }

    public ImageIcon getCorrectPasswords() {
        return correctPasswords;
    }

    public void setCorrectPasswords(ImageIcon correctPasswords) {
        this.correctPasswords = correctPasswords;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public ImageIcon getWrongPasswords() {
        return wrongPasswords;
    }

    public void setWrongPasswords(ImageIcon wrongPasswords) {
        this.wrongPasswords = wrongPasswords;
    }
    
}
